﻿angular.module("umbraco").controller("UE.DealerConfigurationController",
    function ($scope, $route, configurationResource, notificationsService, navigationService, treeService) {
        var vm = this;
        $scope.configuration = {
            platform: "",
            appkey: "",
            listview: false
        };
        vm.buttonState = "init";

        configurationResource.getConfiguration().then(function (response) {
            $scope.configuration = {
                platform: response.MapPlatform,
                appkey: response.MapAppKey,
                listview: response.ListView === "y"
            };
        });

        $scope.saveConfiguration = function () {
            vm.buttonState = "busy";
            configurationResource.saveConfiguration($scope.configuration.platform, $scope.configuration.appkey, $scope.configuration.listview ? "y" : "n").then(function (response) {
                if (response.ErrorCode == 0) {
                    notificationsService.success("Save Success", "Configuration save success!");
                    navigationService.changeSection("dealer");
                    treeService.clearCache({ section: "dealer" });
                    navigationService.syncTree({
                        tree: 'dealerListAlias',
                        path: ["-1"],
                        forceReload: true
                    }).then(function (syncArgs) {
                        //console.log(syncArgs);
                    });
                    $route.reload();
                } else {
                    notificationsService.error("Save Failed", "Configuration save failed, please try again!");
                }
            });
        }
    }
);